<?php
require_once 'includes/header.php';
require_once 'includes/functions.php';
require_once 'includes/DatabaseHelper.php';
require_once '../includes/CognitiveModuleManager.php';

// 初始化认知模块管理器
$moduleManager = new CognitiveModuleManager();

// 获取过滤参数
$ageGroup = isset($_GET['age_group']) ? $_GET['age_group'] : '3-4';

// 处理删除请求
if (isset($_GET['action']) && $_GET['action'] == 'delete' && isset($_GET['id'])) {
    $moduleId = intval($_GET['id']);
    if ($moduleManager->deleteModule($moduleId)) {
        $_SESSION['success_message'] = '认知模块删除成功';
    } else {
        $_SESSION['error_message'] = '认知模块删除失败';
    }
    header('Location: cognitive_modules.php?age_group=' . urlencode($ageGroup));
    exit;
}

// 获取所有认知模块
$db = Database::getInstance();
$sql = "SELECT * FROM cognitive_modules WHERE age_group = ? ORDER BY display_order ASC";
$modules = $db->fetchAll($sql, [$ageGroup]);

// 年龄段选项
$ageGroups = ['3-4', '5-6', '7-8'];
?>

<h1 class="mb-4"><i class="bi bi-journals"></i> 认知模块管理</h1>

<!-- 操作区域 -->
<div class="card mb-4">
    <div class="card-body">
        <div class="row">
            <div class="col-md-6">
                <form method="get" class="form-inline">
                    <div class="input-group">
                        <label class="input-group-text" for="age_group">年龄段:</label>
                        <select class="form-select" id="age_group" name="age_group" onchange="this.form.submit()">
                            <?php foreach ($ageGroups as $group): ?>
                                <option value="<?php echo $group; ?>" <?php echo $group == $ageGroup ? 'selected' : ''; ?>>
                                    <?php echo $group; ?>岁
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                </form>
            </div>
            <div class="col-md-6 text-end">
                <a href="cognitive_module_edit.php?age_group=<?php echo urlencode($ageGroup); ?>" class="btn btn-primary">
                    <i class="bi bi-plus-circle"></i> 添加认知模块
                </a>
            </div>
        </div>
    </div>
</div>

<!-- 消息提示 -->
<?php if (isset($_SESSION['success_message'])): ?>
    <div class="alert alert-success alert-dismissible fade show" role="alert">
        <?php echo $_SESSION['success_message']; ?>
        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
    </div>
    <?php unset($_SESSION['success_message']); ?>
<?php endif; ?>

<?php if (isset($_SESSION['error_message'])): ?>
    <div class="alert alert-danger alert-dismissible fade show" role="alert">
        <?php echo $_SESSION['error_message']; ?>
        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
    </div>
    <?php unset($_SESSION['error_message']); ?>
<?php endif; ?>

<!-- 认知模块列表 -->
<div class="card mb-4">
    <div class="card-header">
        <i class="bi bi-table me-1"></i> <?php echo $ageGroup; ?>岁认知模块列表
    </div>
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-bordered table-hover table-striped">
                <thead class="table-light">
                    <tr>
                        <th>ID</th>
                        <th>模块名称</th>
                        <th>标识符</th>
                        <th>图标</th>
                        <th>显示顺序</th>
                        <th>状态</th>
                        <th width="150">操作</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (count($modules) > 0): ?>
                        <?php foreach ($modules as $module): ?>
                            <tr>
                                <td><?php echo $module['module_id']; ?></td>
                                <td>
                                    <div style="display: flex; align-items: center;">
                                        <div class="color-dot me-2" style="background-color: <?php echo htmlspecialchars($module['background_color']); ?>; border-color: <?php echo htmlspecialchars($module['border_color']); ?>"></div>
                                        <?php echo htmlspecialchars($module['module_name']); ?>
                                    </div>
                                </td>
                                <td><?php echo htmlspecialchars($module['module_key']); ?></td>
                                <td><?php echo htmlspecialchars($module['icon']); ?></td>
                                <td><?php echo $module['display_order']; ?></td>
                                <td>
                                    <?php if ($module['is_active']): ?>
                                        <span class="badge bg-success">显示</span>
                                    <?php else: ?>
                                        <span class="badge bg-secondary">隐藏</span>
                                    <?php endif; ?>
                                </td>
                                <td>
                                    <a href="cognitive_module_edit.php?id=<?php echo $module['module_id']; ?>" class="btn btn-sm btn-primary">
                                        <i class="bi bi-pencil"></i>
                                    </a>
                                    <a href="cognitive_contents.php?module_id=<?php echo $module['module_id']; ?>" class="btn btn-sm btn-info">
                                        <i class="bi bi-list-ul"></i>
                                    </a>
                                    <a href="cognitive_modules.php?action=delete&id=<?php echo $module['module_id']; ?>&age_group=<?php echo urlencode($ageGroup); ?>" class="btn btn-sm btn-danger" onclick="return confirm('确定要删除此模块吗？这将同时删除模块下的所有内容！')">
                                        <i class="bi bi-trash"></i>
                                    </a>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <tr>
                            <td colspan="7" class="text-center">暂无认知模块</td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<!-- 模块的静态页面映射 -->
<div class="card mb-4">
    <div class="card-header">
        <i class="bi bi-link-45deg me-1"></i> 静态页面映射
    </div>
    <div class="card-body">
        <p>以下是当前年龄段认知模块的静态页面映射，您可以通过这些链接预览相应的页面：</p>
        <div class="table-responsive">
            <table class="table table-bordered">
                <thead class="table-light">
                    <tr>
                        <th>模块名称</th>
                        <th>对应静态页面</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($modules as $module): ?>
                        <tr>
                            <td><?php echo htmlspecialchars($module['module_name']); ?></td>
                            <td><code>age-<?php echo $ageGroup; ?>-<?php echo $module['module_key']; ?>.html</code></td>
                            <td>
                                <a href="../age-<?php echo $ageGroup; ?>-<?php echo $module['module_key']; ?>.html" class="btn btn-sm btn-outline-primary" target="_blank">
                                    <i class="bi bi-eye"></i> 预览
                                </a>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<style>
.color-dot {
    width: 20px;
    height: 20px;
    border-radius: 50%;
    border: 2px solid;
}
</style>

<?php require_once 'includes/footer.php'; ?> 